package com.jason.unpluggedsuperstar.ui;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.android.dataframework.DataFramework;
import com.google.ytd.YTD.accessListener;
import com.google.ytd.YTDUploader;
import com.google.ytd.YTDUploader.progressListener;
import com.jason.unpluggedsuperstar.R;
import com.jason.unpluggedsuperstar.UnpluggedSuperStarActivity;
import com.jason.unpluggedsuperstar.utils.VideoUtils;

public class BeAStarVideoList extends ListFragment{
	private VideoAdapter mVideoAdapter;
	private Activity mActivity;
	private BeAStarVideoList mSelf;
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mSelf = this;
        mActivity = this.getActivity();
        initProgressBar();
        
        this.getListView().setCacheColorHint(0x00000000);
        
        try {
			DataFramework.getInstance().open(this.getActivity(), "com.jason.unpluggedsuperstar");
			Cursor c = DataFramework.getInstance().getCursor(VideoUtils.VIDEO_TABLE);
	        
	        mVideoAdapter = new VideoAdapter(this.getActivity() , c , CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
			setListAdapter(mVideoAdapter);
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	private void initProgressBar(){
		UnpluggedSuperStarActivity.mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		UnpluggedSuperStarActivity.mProgressDialog.setMax(100);
		UnpluggedSuperStarActivity.mProgressDialog.setCancelable(false);
		UnpluggedSuperStarActivity.mProgressDialog.setTitle("Uploading...");
		UnpluggedSuperStarActivity.mProgressDialog.setProgress(0);
	}

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("FragmentList", "Item clicked: " + id);
    }
    
    public void reloadVideoList(){
    	this.mVideoAdapter.getCursor().requery();
    }
    
    public class VideoAdapter extends CursorAdapter{

		public VideoAdapter(Context context, Cursor c, int flag) {
			super(context, c , flag);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			TextView tmp = (TextView) view.findViewById(R.id.be_a_star_video_item_title);
			tmp.setText(cursor.getString(cursor.getColumnIndex(VideoUtils.COL_TITLE)));
			
			tmp = (TextView) view.findViewById(R.id.be_a_star_video_item_description);
			tmp.setText(cursor.getString(cursor.getColumnIndex(VideoUtils.COL_DESCRIPTION)));
			
			tmp = (TextView) view.findViewById(R.id.be_a_star_video_item_created_date);
			tmp.setText(cursor.getString(cursor.getColumnIndex(VideoUtils.COL_CREATED_DT)));
			
			tmp = (TextView) view.findViewById(R.id.be_a_star_video_item_duration);
			tmp.setText(cursor.getString(cursor.getColumnIndex(VideoUtils.COL_DURATION)));
			
			tmp = (TextView) view.findViewById(R.id.be_a_star_video_item_status);
			tmp.setText(cursor.getString(cursor.getColumnIndex(VideoUtils.COL_STATUS)));
			
			final String location = cursor.getString(cursor.getColumnIndex(VideoUtils.COL_REAL_LOCATION));
			final String id = cursor.getString(cursor.getColumnIndex(VideoUtils.COL_ID));
			
			Button upload_button = (Button)view.findViewById(R.id.be_a_star_video_item_upload);
			if(tmp.getText().toString().compareTo(VideoUtils.VID_STATUS_CREATED) == 0){
				upload_button.setVisibility(View.VISIBLE);
				upload_button.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						YTDUploader mYTDUploader = new YTDUploader(mActivity);
						mYTDUploader.checkCriteria(new accessListener() {
							@Override
							public void onVerified() {
								Log.e("onVerified", "onVerified");
							}

							@Override
							public void onError(String error) {
								Log.e("error", error);
							}
						});

						mYTDUploader.setProgressListener(new progressListener() {
							@Override
							public void onProgressUpdate(double progress) {
								UnpluggedSuperStarActivity.mProgressDialog.setProgress((int)progress);
								//Log.e("progress", String.valueOf(progress));
							}

							@Override
							public void onError(String error) {
								// TODO Auto-generated method stub
								Log.e("error", error);
							}

							@Override
							public void onSuccess(String videoId) {
								// TODO Auto-generated method stub
								UnpluggedSuperStarActivity.mProgressDialog.hide();
								VideoUtils.updateVideoEntry(mActivity.getApplicationContext(), id);
								mSelf.reloadVideoList();
								
								Log.e("videoId", videoId);
							}
						});
						mYTDUploader.upload(Uri.parse(location),"test 789", "test 789");
						UnpluggedSuperStarActivity.mProgressDialog.show();
					}
					
				});
			}
			else{
				upload_button.setVisibility(View.INVISIBLE);
			}
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup viewgroup) {
			
			LayoutInflater inflater = LayoutInflater.from(context);
			View v = inflater.inflate(R.layout.be_a_star_video_item, null);
			bindView(v, context, cursor);
			
			return v;
		}
    	
    }
}
